<?php
elgg_load_library('elgg:class_organization');
elgg_load_library('elgg:class_tournament');

$entity = new Tournament(get_input('guid'));
$entity->title 		= get_input('name');
$entity->description= get_input('description');
$entity->access_id 	= 2;

if(get_input('org_guid'))
	$org = get_entity(get_input('org_guid'));
else if(get_input('guid'))
{
	$org = $entity->getOrganization();
}

if(!$org->isManager()) forward();

//Save custom fields
$entity->edition					= get_input('edition');
$entity->price						= get_input('price');
$entity->inscription_date_start		= get_input('inscription_date_start');
$entity->inscription_seconds_start 	= get_input('inscription_seconds_start');
$entity->inscription_date_end		= get_input('inscription_date_end');
$entity->inscription_seconds_end	= get_input('inscription_seconds_end');
$entity->timetables 				= get_timetables();
$entity->prizes						= get_prizes();

if($entity->save())
{
	check_avatar($entity);
	
	//Add the tournament to the organization
	if($org->guid)
	{
		$org->addTournament($entity);
	}
	
	system_message("Torneo guardado");
}
else
{
	register_error("Error");
}

forward("tournaments/edit/".$entity->guid);


function check_avatar($entity)
{
	// Now see if we have a file icon
	if ((isset($_FILES['icon'])) && (substr_count($_FILES['icon']['type'],'image/'))) {
		
		$icon_sizes = elgg_get_config('icon_sizes');
		$prefix = "tournaments/".$entity->guid;
	
		$filehandler = new ElggFile();
		$filehandler->owner_guid = $entity->owner_guid;
		$filehandler->setFilename($prefix . ".jpg");
		$filehandler->open("write");
		$filehandler->write(get_uploaded_file('icon'));
		$filehandler->close();
	
		$thumbtiny = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['tiny']['w'], $icon_sizes['tiny']['h'], $icon_sizes['tiny']['square']);
		$thumbsmall = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['small']['w'], $icon_sizes['small']['h'], $icon_sizes['small']['square']);
		$thumbmedium = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['medium']['w'], $icon_sizes['medium']['h'], $icon_sizes['medium']['square']);
		$thumblarge = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['large']['w'], $icon_sizes['large']['h'], $icon_sizes['large']['square']);
		
		if ($thumbtiny)
		{
			$thumb = new ElggFile();
			$thumb->owner_guid = $entity->owner_guid;
			$thumb->setMimeType('image/jpeg');
	
			$thumb->setFilename($prefix."tiny.jpg");
			$thumb->open("write");
			$thumb->write($thumbtiny);
			$thumb->close();
	
			$thumb->setFilename($prefix."small.jpg");
			$thumb->open("write");
			$thumb->write($thumbsmall);
			$thumb->close();
	
			$thumb->setFilename($prefix."medium.jpg");
			$thumb->open("write");
			$thumb->write($thumbmedium);
			$thumb->close();
	
			$thumb->setFilename($prefix."large.jpg");
			$thumb->open("write");
			$thumb->write($thumblarge);
			$thumb->close();
	
			$entity->setMetaData('icontime', time());
		}
	}
}
function get_timetables()
{
	$inputDateFormat 	= "timetable-date-";
	$inputStartFormat 	= "timetable-start-";
	$inputEndFormat 	= "timetable-end-";
	
	$timetables = array();
	
	//Get the inputs data 6 store them in array
	foreach ($_POST as $key => $value)
	{
		if(strstr($key, $inputDateFormat) && $value != '')
		{
			//Get the id of the date
			$aux = explode('-',$key);
			$id = $aux[2];
			
			$timetables[] = $value.';'.$_POST[$inputStartFormat.$id].';'.$_POST[$inputEndFormat.$id];
		}
	}
	
	return $timetables;
}
function get_prizes()
{
	$inputCondition 	= 'prize-condition';
	$inputDescription 	= 'prize-description';
	
	$prizes = array();
	
	foreach($_POST[$inputCondition] as $key => $value)
	{
		if($value)
		{
			$prizes[] = $value.';'.$_POST[$inputDescription][$key];
		}
	}
	
	return $prizes;
}
